#! /usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from statusbar import StatusBar
from editor import CodeEditor
from tabs import TabPane
from filter_dlg import FilterTrhouhgCommandDialog
from pprint import pprint
from dialogs import GoToLineDialog

class SymbolView(QDockWidget):
    def __init__(self):
        self.setWindowTitle('Symbols')
        layout = QVBoxLayout()
        self.setLayout(layout)
        self.searchLine = QLineEdit()
        layout.addWidget(self.searchLine)

class FileSystemView(QDockWidget):
    pass

class TerminalView(QDockWidget):
    # This is a musthave :)
    pass

class DocumentTabWidget(QTabWidget):
    pass

    

class CrossMatesWindow(QMainWindow):
    '''
    CrossMates main window.
    Right now I can't tell if it'll be useful to support many windows.
    Maybe one per project as TextMate does...?
    '''
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowIcon(QIcon(':/res/winicon.png'))
        self.setWindowTitle('CrossMates')
        self.setMinimumSize(QSize(560, 380))

        self.setup_actions()
        self.setup_menubar()
        self.setup_dialogs()
        
        self.statusbar = StatusBar(self)
        self.tabPane = TabPane(self)
        self.setCentralWidget(self.tabPane)
        
        QMetaObject.connectSlotsByName(self)
    
    def setup_dialogs(self):
        '''
        Setup some dialogs.
        '''
        self.filter_command_dlg = FilterTrhouhgCommandDialog(self)
        self.go_to_line_dlg = GoToLineDialog(self)
        
    def setup_menubar(self):
        self.menubar = QMenuBar(self)
        self.setMenuBar(self.menubar)
        fileMenu = self.menubar.addMenu('&File')
        fileMenu.addAction(self.actionNewFile)
        fileMenu.addAction(self.actionOpenFile)
        menuOpenRecent = fileMenu.addMenu('Open &Recent')
        menuNewFromTemplate = fileMenu.addAction(self.actionSaveFile)
        fileMenu.addAction(self.actionCloseFile)
        fileMenu.addAction(self.actionSaveFile)
        fileMenu.addAction(self.actionSaveFileAs)
        
        fileMenu.addSeparator()
        
        fileMenu.addAction(self.actionNewProject)
        fileMenu.addAction(self.actionOpenFolderAsProject)
        fileMenu.addAction(self.actionSaveProject)
        fileMenu.addAction(self.actionSaveProjectAs)
        fileMenu.addAction(self.actionRevealInProject)
        fileMenu.addAction(self.actionCloseProject)
        
        fileMenu.addSeparator()
        
        fileMenu.addAction(self.actionQuit)
        
        
        menuEdit = self.menubar.addMenu('&Edit')
        menuEdit.addAction(self.actionUndo)
        menuEdit.addAction(self.actionRedo)
        menuEdit.addSeparator()
        menuEdit.addAction(self.actionCut)
        menuEdit.addAction(self.actionCopy)
        menuEdit.addAction(self.actionPaste)
        
        menuEdit.addAction(self.actionPastePrevious)
        menuEdit.addAction(self.actionPasteFromHistory)
        
        menuEdit.addSeparator()
        menuEdit.addMenu('Mode')
        menuEdit.addMenu('Select')
        menuEdit.addSeparator()
        menuEdit.addMenu('Find')
        menuEdit.addMenu('Spelling')
        menuEdit.addMenu('Completion')
        menuEdit.addMenu('Special Characters')
        
        
        viewMenu = self.menubar.addMenu('&View')
        fontMenu = viewMenu.addMenu('&Font')
        fontMenu.addAction(self.actionZoomIn)
        fontMenu.addAction(self.actionZoomOut)
        
        gutterMenu = viewMenu.addMenu('&Gutter')
        gutterMenu.addAction(self.actionShowLineNumbers)
        gutterMenu.addAction(self.actionShowFoldings)
        gutterMenu.addAction(self.actionShowBookmarks)
        
        
        #optionsMenu = self.menubar.addMenu('&Options')
        #optionsMenu.addAction(self.actionShowLineNumbers)
        
        
        textMenu = self.menubar.addMenu('&Text')
        textMenu.addMenu('&Align')
        textMenu.addMenu('&Convert')
        textMenu.addMenu('&Move Selection')
        textMenu.addSeparator()
        textMenu.addAction(self.actionShiftLeft)
        textMenu.addAction(self.actionShiftRight)
        textMenu.addSeparator()
        
        textMenu.addAction(self.actionEditEachLineInSelection)
        textMenu.addAction(self.actionExecuteSelection)
        textMenu.addAction(self.actionFilterThroughCommand)
        
        navMenu = self.menubar.addMenu('&Navigation')
        navMenu.addAction(self.actionToggleBookmark)
        navMenu.addAction(self.actionNextBookmark)
        navMenu.addAction(self.actionPrevBookmark)
        navMenu.addAction(self.actionGoToBookmark)
        navMenu.addSeparator()
        
        navMenu.addAction(self.actionNextFileTab)
        navMenu.addAction(self.actionPrevFileTab)
        navMenu.addSeparator()
        
        navMenu.addAction(self.actionGoToHeadSource)
        navMenu.addAction(self.actionGoToFile)
        navMenu.addAction(self.actionGoToSymbol)
        navMenu.addAction(self.actionGoToTab)
        navMenu.addAction(self.actionGoToLine)
        
        bundleMenu = self.menubar.addMenu('&Bundles')
        bundleMenu.addAction(self.actionShowBundles)
        bundleMenu.addAction(self.actionShowBundleEditor)
        bundleMenu.addSeparator()
        
        helpMenu = self.menubar.addMenu('&Help')
        helpMenu.addAction(self.actionHelp)
        helpMenu.addAction(self.actionRequestAFeature)
        helpMenu.addAction(self.actionAbout)
        
    def setup_actions(self):
        
        # ======================================================================
        # File actions
        # ======================================================================
        self.actionNewFile = QAction('&New', self)
        self.actionNewFile.setObjectName('actionNewFile')
        self.actionNewFile.setShortcut('Ctrl+N')
        
        self.actionNewFromTemplate = QAction('New From &Template', self)
        self.actionNewFromTemplate.setObjectName('actionNewFromTemplate')
        self.actionNewFromTemplate.setShortcut('Ctrl+Shift+N')
        
        self.actionOpenFile = QAction('&Open File', self)
        self.actionOpenFile.setObjectName('actionOpenFile')
        self.actionOpenFile.setShortcut('Ctrl+O')
        
        self.actionCloseFile = QAction('&Close', self)
        self.actionCloseFile.setObjectName('actionCloseFile')
        self.actionCloseFile.setShortcut('Ctrl+W')
        
        self.actionSaveFile = QAction('&Save', self)
        self.actionSaveFile.setObjectName('actionSaveFile')
        self.actionSaveFile.setShortcut('Ctrl+S')
        
        self.actionSaveFileAs = QAction('Save &As...', self)
        self.actionSaveFileAs.setObjectName('actionSaveFileAs')
        self.actionSaveFileAs.setShortcut('Ctrl+Shift+S')
        
        # ======================================================================
        # File -> Project actions
        # ======================================================================
        
        self.actionNewProject = QAction('New Project', self)
        self.actionNewProject.setObjectName('actionNewProject')
        self.actionNewProject.setShortcut('Ctrl+Shift+N')
        
        self.actionOpenFolderAsProject = QAction('Open Folder As Project', self)
        self.actionOpenFolderAsProject.setObjectName('actionOpenFolderAsProject')
        self.actionOpenFolderAsProject.setShortcut('Ctrl+Shift+N')
        
        self.actionSaveProject = QAction('Save Project', self)
        self.actionSaveProject.setObjectName('actionSaveProject')
        self.actionSaveProject.setShortcut('Ctrl+Shift+S')
        
        
        self.actionSaveProjectAs = QAction('Save Project As', self)
        self.actionSaveProjectAs.setObjectName('actionSaveProjectAs')
        #self.actionSaveProjectAs.setShortcut('Ctrl+Shift+N')
        
        self.actionRevealInProject = QAction('Reveal in project', self)
        self.actionRevealInProject.setObjectName('actionRevealInProject')
        self.actionRevealInProject.setShortcut('Ctrl+Shift+P')
        
        
        
        self.actionCloseProject = QAction('Close project', self)
        self.actionCloseProject.setObjectName('actionCloseProject')
        self.actionCloseProject.setShortcut('Ctrl+Alt+W')
        
        self.actionQuit = QAction('&Quit', self)
        self.actionQuit.setObjectName('Quit')
        self.actionQuit.setShortcut('Ctrl+Q')
        
        # ======================================================================
        # Edit actions
        # ======================================================================
        
        self.actionUndo = QAction('&Undo', self)
        self.actionUndo.setShortcut('Ctrl+Z')
        
        self.actionRedo = QAction('&Redo', self)
        self.actionRedo.setShortcut('Ctrl+Y')
        
        self.actionCut = QAction('&Cut', self)
        self.actionCut.setShortcut('Ctrl+X')
        self.actionCut.setObjectName('actionCut')
        
        self.actionCopy = QAction('C&opy', self)
        self.actionCopy.setShortcut('Ctrl+C')
        self.actionCopy.setObjectName('actionCopy')
        
        self.actionPaste = QAction('&Paste', self)
        self.actionPaste.setShortcut('Ctrl+V')
        self.actionPaste.setObjectName('actionPaste')
        
        
        self.actionPastePrevious = QAction('Paste P&revious', self)
        self.actionPastePrevious.setObjectName('actionPastePrevious')
        
        self.actionPasteFromHistory = QAction('Paste From &History', self)
        self.actionPasteFromHistory.setObjectName('actionPasteFromHistory')
        
        # ======================================================================
        # View->Gutter actions
        # ======================================================================
        
        # Font
        
        self.actionZoomIn = QAction('Zoom &In', self)
        self.actionZoomIn.setObjectName('actionZoomIn')
        self.actionZoomIn.setShortcut('Ctrl++')
        
        self.actionZoomOut = QAction('Zoom &Out', self)
        self.actionZoomOut.setObjectName('actionZoomOut')
        self.actionZoomOut.setShortcut('Ctrl+-')
        
        # Gutter
        self.actionShowLineNumbers = QAction('&Show line numbers', self)
        self.actionShowLineNumbers.setObjectName('actionShowLineNumbers')
        self.actionShowLineNumbers.setCheckable(True)
        self.actionShowLineNumbers.setChecked(True)
        self.actionShowLineNumbers.setShortcut('F11')
        
        self.actionShowFoldings = QAction('Show &Folding', self)
        self.actionShowFoldings.setObjectName('actionShowFoldings')
        self.actionShowFoldings.setCheckable(True)
        self.actionShowFoldings.setChecked(True)
        self.actionShowFoldings.setShortcut('F10')
        
        self.actionShowBookmarks = QAction('Show &Bookmarks', self)
        self.actionShowBookmarks.setObjectName('actionShowBookmarks')
        self.actionShowBookmarks.setCheckable(True)
        self.actionShowBookmarks.setChecked(True)
        self.actionShowBookmarks.setShortcut('Shift+F10')
        
        self.actionShiftLeft = QAction('Shift Left', self)
        self.actionShiftLeft.setObjectName('actionShiftLeft')
        
        # ======================================================================
        # Text Menu
        # ======================================================================
        
        self.actionShiftRight = QAction('Shift Right', self)
        self.actionShiftLeft.setObjectName('actionShiftRight')
        
        
        self.actionEditEachLineInSelection = QAction('Edit Each Line In Selection', self)
        self.actionEditEachLineInSelection.setCheckable(True)
        self.actionEditEachLineInSelection.setChecked(False)
        
        
        self.actionFilterThroughCommand = QAction('Filter through command', self)
        self.actionFilterThroughCommand.setObjectName('actionFilterThroughCommand')
        self.actionFilterThroughCommand.setShortcut('Ctrl+Shift+R')
        
        self.actionExecuteSelection = QAction('Execute selection inserting result', self)
        self.actionExecuteSelection.setObjectName('actionExecuteSelection')
        
        # ======================================================================
        # Navigation menu
        # ======================================================================
        
        self.actionToggleBookmark = QAction('Toggle bookmark', self)
        self.actionNextBookmark = QAction('Next Bookmark', self)
        self.actionPrevBookmark = QAction('Previous Bookmark', self)
        self.actionGoToBookmark = QAction('Go To Bookmark', self)
        
        self.actionNextFileTab = QAction('Next File Tab', self)
        self.actionNextFileTab.setObjectName('actionNextFileTab')
        self.actionNextFileTab.setShortcut('Alt+Right')
        
        self.actionPrevFileTab = QAction('Previous File Tab', self)
        self.actionPrevFileTab.setObjectName('actionPrevFileTab')
        self.actionPrevFileTab.setShortcut('Alt+Left')
        
        self.actionGoToHeadSource = QAction('Go To Head/Source', self)
        self.actionGoToHeadSource.setObjectName('actionGoToHeadSource')
        
        self.actionGoToFile = QAction('Go To File', self)
        self.actionGoToFile.setObjectName('actionGoToFile')
        
        self.actionGoToSymbol = QAction('Go To Symbol', self)
        self.actionGoToSymbol.setObjectName('actionGoToSymbol')
        
        self.actionGoToTab = QAction('Go To Tab', self)
        self.actionGoToTab.setObjectName('actionGoToTab')
        
        self.actionGoToLine = QAction('Go To Line', self)
        self.actionGoToLine.setObjectName('actionGoToLine')
        self.actionGoToLine.setShortcut('Ctrl+G')
        
        # ======================================================================
        # Bundles Actions
        # ======================================================================
        self.actionShowBundles = QAction('Select Bundle', self)
        self.actionShowBundles.setObjectName('actionShowBundles')
        self.actionShowBundles.setShortcut('Ctrl+Alt+T')
        
        
        self.actionShowBundleEditor = QAction('Show Bundles', self)
        self.actionShowBundleEditor.setObjectName('actionShowBundleEditor')
        self.actionShowBundleEditor.setShortcut('Ctrl+Alt+B')
        
        # ======================================================================
        # Help Actions
        # ======================================================================
        self.actionAbout = QAction('&About', self)
        self.actionAbout.setObjectName('actionAbout')
        
        self.actionRequestAFeature = QAction('&Request a feature', self)
        self.actionRequestAFeature.setObjectName('actionRequestAFeature')
        
        self.actionHelp = QAction('&Help', self)
        self.actionHelp.setObjectName('actionHelp')
        
    @pyqtSignature('')
    def on_actionFilterThroughCommand_triggered(self):
        self.filter_command_dlg.exec_()
        
        
    @pyqtSignature('')
    def on_actionOpenFile_triggered(self):
        index = None
        files = QFileDialog.getOpenFileNames(self, "Open", "Open files")
        for f in files:
            
            url = QUrl()
            url.setScheme('file')
            url.setPath(f)
            index = self.centralWidget().factory.get_editor(url)
        if index is not None:
            self.centralWidget().setCurrentIndex(index)
            
    
    @pyqtSignature('bool')
    def on_actionShowLineNumbers_toggled(self, checked):
        widget = self.centralWidget().currentWidget()
        if widget:
            widget.linenumberEnabled = checked
        
    @pyqtSignature('bool')
    def on_actionShowFoldings_toggled(self, checked):
        widget = self.centralWidget().currentWidget()
        if widget:
            widget.foldingEnabled = checked
    
    @pyqtSignature('bool')
    def on_actionShowBookmarks_toggled(self, checked):
        widget = self.centralWidget().currentWidget()
        if widget:
            widget.bookmarkEnabled = checked
    
    @pyqtSignature('')
    def on_actionNewFile_triggered(self):
        tabs = self.centralWidget()
        print tabs
        try:
            tabs.insert_new()
        except Exception, e:
            print e
    
    @pyqtSignature('')
    def on_actionCloseFile_triggered(self):
        tabs = self.centralWidget()
        tabs.closeCurrent()
    
    @pyqtSignature('')
    def on_actionQuit_triggered(self):
        #tabs = self.centralWidget()
        #tabs.closeCurrent()
        res = QMessageBox.question(self, 'Close', 
                                'Are you sure you want to close this app?',
                                QMessageBox.Yes | QMessageBox.No )
        if res == QMessageBox.Yes:
            qApp.instance().quit()
    
    @pyqtSignature('')
    def on_actionZoomIn_triggered(self):
        code_editor = self.centralWidget().currentWidget()
        if code_editor:
            #print code_editor.editor
            print "Zooming"
            code_editor.editor.zoomIn()
    
    @pyqtSignature('')
    def on_actionZoomOut_triggered(self):
        code_editor = self.centralWidget().currentWidget()
        if code_editor:
            #print code_editor.editor
            print "Zooming"
            code_editor.editor.zoomOut()
    
    
    @pyqtSignature('')
    def on_actionAbout_triggered(self):
        QMessageBox.about(self, "About CrossMates", """
        <h4>CrossMates</h4>
        <p>A programmer's text editor based on TextMate's bundles and
        some Kate goodies.</p>
        <p>CrossMates aims to be a practical editor so suggest your improvements.</p>
        <p>CrossMates is developed under PyQt4.</p>
        """)
    
    @pyqtSignature('')
    def on_actionNextFileTab_triggered(self):
        tabPane = self.centralWidget()
        current = tabPane.currentIndex() + 1
        if current >= tabPane.count():
            current = 0
        tabPane.setCurrentIndex(current)
        
        
    @pyqtSignature('')
    def on_actionPrevFileTab_triggered(self):
        tabPane = self.centralWidget()
        current = tabPane.currentIndex() - 1
        if current < 0:
            current = tabPane.count() - 1
        tabPane.setCurrentIndex(current)
        
    @pyqtSignature('')
    def on_actionGoToLine_triggered(self):
        code_editor = self.centralWidget().currentWidget()
        if code_editor:
            self.go_to_line_dlg.exec_(code_editor)    
            
            #QInputDialog.getInteger(self, "Line number", "Go to line number")
    @pyqtSignature('')
    def on_actionShowBundles_triggered(self):
        QMessageBox.information(self, "Bundles", "Bundles")
    
    
    @pyqtSignature('')
    def on_actionShowBundleEditor_triggered(self):
        qApp.instance().bundleEdtorWindow.show()
        
# Resources
import resources_rc